package com.yanqu.road.server.cmd.activity.doublespring;

import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.DaoMuPvpProto;
import com.yanqu.road.pb.activity.DoubleSpringProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.doublespring.CrossDoubleSpringActivity;
import com.yanqu.road.server.manager.activity.doublespring.CrossDoubleSpringActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C_CROSS_DOUBLE_SPRING_AGREE_INVITE, desc = "踏春游记-同意邀请")
public class CrossDoubleSpringAgreeInviteCmd extends CrossCommand {

    private long playerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        playerId = packet.getPlayerId();
        return true;
    }
    
    @Override
    public void execute(YanQuMessage packet) throws Exception {
        DoubleSpringProto.DoubleSpringAgreeInviteReqMsg reqMsg = DoubleSpringProto.DoubleSpringAgreeInviteReqMsg.parseFrom(packet.getBytes());
        CrossDoubleSpringActivity activity = CrossDoubleSpringActivityMgr.getActivity(reqMsg.getActivityId());
        if (activity == null) {
            ret = GameErrorCode.E_DOUBLE_SPRING_ACTIVITY_NOT_EXIST;
            return;
        }
        try {
            //同意邀请
            activity.agreeInvite(reqMsg.getActivityId(), packet.getPlayerId(), reqMsg.getUserId(), serverId, reqMsg.getServerId());
            ret = 0;
        } catch (BusinessException e) {
            ret = e.getCode();
        }
    }

    @Override
    public void sendData() {
        DoubleSpringProto.DoubleSpringAgreeInviteRespMsg.Builder respMsg = DoubleSpringProto.DoubleSpringAgreeInviteRespMsg.newBuilder();
        respMsg.setRet(ret);
        MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_DOUBLE_SPRING_AGREE_INVITE, respMsg));
    }
}
